// 预存示例学生数据，学号以 '2' 开头
function seedStudents() {
  const sampleStudents = [
    { userid: "20190001", username: "Zhang San", gender: "male" },
    { userid: "20190002", username: "Li Si", gender: "female" },
    { userid: "20190003", username: "Wang Wu", gender: "male" }
  ];
  sampleStudents.forEach(student => {
    if (!localStorage.getItem(student.userid)) {
      localStorage.setItem(student.userid, JSON.stringify(student));
    }
  });
}

// 渲染学生列表（只读，不含手机号）
function renderStudentList() {
  const tbody = document.getElementById('student-list-tbody');
  const students = [];
  for (let i = 0; i < localStorage.length; i++) {
    const key = localStorage.key(i);
    if (key.startsWith('2')) {
      try {
        const student = JSON.parse(localStorage.getItem(key));
        students.push(student);
      } catch (e) { }
    }
  }
  tbody.innerHTML = students.map(s => `
    <tr>
      <td>${s.userid || ''}</td>
      <td>${s.username || ''}</td>
      <td>${s.gender || ''}</td>
    </tr>
  `).join('');
}

// 初始化老师信息表单
function loadTeacherInfo() {
  const teacher = JSON.parse(localStorage.getItem('teacher'));
  if (!teacher) return;
  document.getElementById('teacher-username').value = teacher.userid || '';
  const genderRadios = document.querySelectorAll('input[name="gender"]');
  genderRadios.forEach(radio => {
    radio.checked = (teacher.gender === radio.value);
  });
  document.getElementById('teacher-phone').value = teacher.phone || '';
  document.getElementById('teacher-email').value = teacher.email || '';
}

// 标签切换逻辑
const tabStudentBtn = document.getElementById('tab-student');
const tabTeacherBtn = document.getElementById('tab-teacher');
const studentSection = document.getElementById('student-section');
const teacherSection = document.getElementById('teacher-section');

tabStudentBtn.addEventListener('click', () => {
  tabStudentBtn.classList.add('active');
  tabTeacherBtn.classList.remove('active');
  tabStudentBtn.style.borderBottom = '3px solid #0066cc';
  tabStudentBtn.style.backgroundColor = '#e6f0ff';
  tabTeacherBtn.style.borderBottom = '3px solid transparent';
  tabTeacherBtn.style.backgroundColor = '#f9fafb';

  studentSection.style.display = '';
  teacherSection.style.display = 'none';
});

tabTeacherBtn.addEventListener('click', () => {
  tabTeacherBtn.classList.add('active');
  tabStudentBtn.classList.remove('active');
  tabTeacherBtn.style.borderBottom = '3px solid #0066cc';
  tabTeacherBtn.style.backgroundColor = '#e6f0ff';
  tabStudentBtn.style.borderBottom = '3px solid transparent';
  tabStudentBtn.style.backgroundColor = '#f9fafb';

  teacherSection.style.display = '';
  studentSection.style.display = 'none';
});

// 保存老师信息
document.getElementById('teacher-info-form').addEventListener('submit', function (e) {
  e.preventDefault();
  const teacher = JSON.parse(localStorage.getItem('teacher')) || {};
  teacher.gender = document.querySelector('input[name="gender"]:checked')?.value || '';
  teacher.phone = document.getElementById('teacher-phone').value.trim();
  teacher.email = document.getElementById('teacher-email').value.trim();

  const phonePattern = /^1[3-9]\d{9}$/;
  if (teacher.phone && !phonePattern.test(teacher.phone)) {
    alert('Please enter a valid phone number');
    return;
  }
  const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (teacher.email && !emailPattern.test(teacher.email)) {
    alert('Please enter a valid email address');
    return;
  }

  localStorage.setItem('teacher', JSON.stringify(teacher));
  alert('Information saved successfully!');
});

// 页面初始化
window.onload = function () {
  seedStudents();
  renderStudentList();
  loadTeacherInfo();
};
